package com.wang.controller;

import com.wang.pojo.Books;
import com.wang.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/books")
public class BookController {


    private BookService bookService;

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @RequestMapping("/allbook")
    public String queryAllBook(Model model){
        List<Books> booksList=bookService.queryAllBook();
        model.addAttribute("list",booksList);
        return "allbooks";
    }

    //跳转到添加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addBook";
    }
    //添加书籍功能
    @RequestMapping("/addBook")
    public String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/books/allbook";
    }


    //跳转到修改图书界面
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(int id,Model model){
       Books qeBook= bookService.queryBookById(id);
        model.addAttribute("qeBook",qeBook);
        return "updateBook";
    }
    //更改书籍信息
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        bookService.updateBook(books);
        return "redirect:/books/allbook";
    }
    //删除书籍
    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        bookService.deleteBookById(id);
        return "redirect:/books/allbook";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public  String queryBook(String bookName,Model model){
        Books book=bookService.queryBookByName(bookName);
        List<Books> list=new ArrayList<Books>();
        list.add(book);
        model.addAttribute("list",list);
        return "allbooks";
    }
}
